import java.sql.SQLException;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;

/**
 * ViewMedicalHistory
 * Patient Medical History - 
 * Allows patients to see all of their treatment records on file
 * 
 * @author bethanysumner
 *
 * @version $Revision: 1.0 $
 */
public class ViewMedicalHistory extends JPanel{
	
	/**
	 * Field main.
	 */
	private MainWindow main;
	/**
	 * Field database.
	 */
	private Database database;
	/**
	 * Field user.
	 */
	private String user;
	
	/**
	 * Field table.
	 */
	private JTable table;
	/**
	 * Field model.
	 */
	private DefaultTableModel model;
	
	/**
	 * ViewMedicalHistory Constructor
	 * @param user the username of whom to view his/her medical history
	 * @param main MainWindow
	 * @param database Database
	 */
	public ViewMedicalHistory(MainWindow main, Database database, String user) {
		
		this.main = main;
		this.database = database;
		this.user = user;
		
		setLayout(null);
		
		assert(main.getUsertype() == 3);
		showPatientsTreatmentRecords(main.getCurrentUser());
	
	}
	
	/**
	 * Method showPatientsTreatmentRecords.
	 * @param username String
	 */
	public void showPatientsTreatmentRecords(String username){
		String[] columnNames = {"Doctor", "Nurse", "Date", "Time", "PatientID", "Medicine", "Lab Work", "Instructions",
				"Complain", "BloodPressure(D)", "BloodPressure(S)", "Pulse", "Temperature", "Height", "Weight", "Diagnosis"};
		String[][] data;

		try {
			data = database.requestMedicalHistory(username);
			addTable(data, columnNames);
		} catch (SQLException e) {
			main.showError("Error Connecting with Database");
			e.printStackTrace();
		}
		
	}
	
	/**
	 * Cretes the table with the given data and columnNames
	 * @param data Data to populate the table
	 * @param columnNames Column Names
	 */
	private void addTable(String[][] data, String[] columnNames){
		
		model = new DefaultTableModel(data, columnNames);
		table = new JTable(model);
		//tableListener = new TableListener();
		//model.addTableModelListener(tableListener);
		
		JScrollPane scrollPane = new JScrollPane(table);
		scrollPane.setBounds(20, 60, 1000, 500);
		add(scrollPane);
		
	}
	

}
